<?php
/**
 * 文章类型模型类
 * @author caoge
 * @version 2012-5-7
 */
class ArticleTypeModel extends BaseModel{
	/**
	 * 获取文章分类数据
	 * 
	 * @author caoge
	 * @version 2012-7-15
	 */
	public function getTypeList(){
		return $this->where('del_flag=0')->order('sort desc')->select();
	}
	
	/**
	 * 统计每种类别里文章数
	 * 
	 * @author caoge
	 * @version 2012-7-16
	 */
	public function categoryCount(){
		//先统计所有文章的分类
		$articleCountSql = 'select type_id,count(1) as num from blog_article where del_flag=0 group by type_id';
		$articleCount = $this->query($articleCountSql);
		$ret = $this->getTypeList();
		array_unshift($ret,array('type_id'=>0,'type_name'=>'默认分类'));
		foreach ($ret as &$v){
			if (!$v['count']){
				$v['count'] = 0;
			}
			foreach ($articleCount as $val){
				if($v['type_id'] == $val['type_id']){
					$v['count'] = $val['num'];
				}
			}
		}
		return $ret;
	}
}